package stu.officedrunker.applications.common.exception;

import org.springframework.context.MessageSourceResolvable;

import stu.officedrunker.applications.common.CommonResultCode;
import stu.officedrunker.applications.common.CommonResultMessage;

/**
 * 와글 예외 처리를 위한 클래스
 * 
 * @author 서성수
 * @see CommonResultCode
 * @see CommonResultMessage
 */
@SuppressWarnings("serial")
public class CommonException extends RuntimeException implements MessageSourceResolvable {
	
	private CommonResultMessage resultMessage;

	// Constructors

	/**
	 * 생성자
	 */
	public CommonException() {
		this(null, null);
	}

	/**
	 * 생성자
	 * 
	 * @param cause
	 *            the cause
	 */
	public CommonException(Throwable cause) {
		this(null, cause);
	}

	/**
	 * 생성자
	 * 
	 * @param resultMessage
	 *            {@link CommonResultMessage}
	 */
	public CommonException(CommonResultMessage resultMessage) {
		this(resultMessage, null);
	}

	/**
	 * 생성자
	 * 
	 * @param resultMessage
	 *            {@link CommonResultMessage}
	 * @param cause
	 *            the cause
	 */

	public CommonException(CommonResultMessage resultMessage, Throwable cause) {
		super(cause);
		if (resultMessage != null) {
			this.resultMessage = resultMessage;
		} else {
			this.resultMessage = CommonResultCode.UNKNOWN_ERROR.message();
		}
	}

	// Auto generate getters

	/**
	 * Get the {@link CommonResultCode}
	 */
	public CommonResultCode getResultCode() {
		return resultMessage.getResultCode();
	}

	/**
	 * Get the {@link CommonResultMessage}
	 */
	public CommonResultMessage getResultMessage() {
		return resultMessage;
	}

	// Throwable Override/Implement methods

	@Override
	public String getMessage() {
		return resultMessage.toString();
	}

	// MessageSourceResolvable Override/Implement methods

	//@Override
	public String[] getCodes() {
		return resultMessage.getCodes();
	}

	//@Override
	public Object[] getArguments() {
		return resultMessage.getArguments();
	}

	//@Override
	public String getDefaultMessage() {
		return resultMessage.getDefaultMessage();
	}
	
}